﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IAATO
{
    public partial class UtilisateurManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblErrorUserName.Text = "";

            #region binding gridView

            MembershipUserCollection users = Membership.GetAllUsers();
            if (users == null)
            {
                lblError.Text = "An error has occurred";
            }
            else if (users.Count == 0)
            {
                lblError.Text = "No user in the database";
                gdwUtilisateurs.DataBind();
            }

            else
            {
                lblError.Text = "";
                gdwUtilisateurs.DataSource = users;
                gdwUtilisateurs.DataBind();
            }

            if (!IsPostBack)
            {
                ddlRole.DataSource = Roles.GetAllRoles();
                ddlRole.DataBind();
            }

            #endregion
        }


        protected void btnAjouter_Click(object sender, EventArgs e)
        {
            try
            {
                Membership.CreateUser(txtNom.Text, txtNom.Text + "password", txtEmail.Text);
                Roles.AddUserToRole(txtNom.Text, ddlRole.SelectedValue);

                Response.Redirect("UtilisateurManagement.aspx");
            }
            catch (MembershipCreateUserException ex)
            {
                lblErrorUserName.Text = ex.StatusCode.ToString();
            }
        }

        protected void btnModifier_Click(object sender, EventArgs e)
        {
            MembershipUser user = Membership.GetUser(userNameUpdate.Value);
            user.Email = txtEmail.Text;
            Membership.UpdateUser(user);

            string currentRole = Roles.GetRolesForUser(txtNom.Text).First();

            if (currentRole != ddlRole.SelectedValue)
            {
                Roles.RemoveUserFromRole(txtNom.Text, currentRole);
                Roles.AddUserToRole(txtNom.Text, ddlRole.SelectedValue);
            }

            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;
            Response.Redirect("UtilisateurManagement.aspx");
        }

        protected void btnAnnuler_Click(object sender, EventArgs e)
        {
            txtNom.Enabled = true;
            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;

            txtNom.Text = "";
            txtEmail.Text = "";
        }

        protected void update_Click(object sender, EventArgs e)
        {
            MembershipUser user = Membership.GetUser(((ImageButton)sender).CommandArgument);

            txtNom.Text = user.UserName;
            txtNom.Enabled = false;
            txtEmail.Text = user.Email;
            ddlRole.SelectedValue = Roles.GetRolesForUser(txtNom.Text).First();

            btnAjouter.Visible = false;
            btnModifier.Visible = true;
            btnAnnuler.Visible = true;

            userNameUpdate.Value = txtNom.Text;

            hdfScrollIntoView.Value = "True";
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            Membership.DeleteUser(((ImageButton)sender).CommandArgument);
            Response.Redirect("UtilisateurManagement.aspx");
        }

        protected void gdwUtilisateurs_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdwUtilisateurs.DataSource = Membership.GetAllUsers();
            gdwUtilisateurs.PageIndex = e.NewPageIndex;
            gdwUtilisateurs.DataBind(); 
        }
    }
}